var map;
var markersArray = [];
var image;
var geocoder;
function initialize(currentLocation,element,element_txt_x,element_txt_y) {
  geocoder = new google.maps.Geocoder();
  var myLatlng = currentLocation;
  var myOptions = {
    zoom: 14,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById(element), myOptions);
  
  google.maps.event.addListener(map, 'click', function(event) {
	deleteMarkers();
	var tempLatLng = event.latLng;
    placeMarker(tempLatLng);
	document.getElementById(element_txt_x).value = ""+tempLatLng.lat()+ "";
	document.getElementById(element_txt_y).value = ""+tempLatLng.lng()+ "";
  });
}
  
function codeAddress(txt_address,element_txt_x,element_txt_y) {
	var address = document.getElementById(txt_address).value;
	geocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			deleteMarkers();
			var tempLatLng = results[0].geometry.location;
			placeMarker(tempLatLng);
			document.getElementById(element_txt_x).value = ""+tempLatLng.lat()+ "";
			document.getElementById(element_txt_y).value = ""+tempLatLng.lng()+ "";
  		} else {
			alert("Geocode was not successful for the following reason: " + status);
  		}
	});
}
  
function placeMarker(location) {
  var marker = new google.maps.Marker({
      position: location, 
	  icon:image,
      map: map
  });
  markersArray.push(marker);
  map.setCenter(location);
}

function deleteMarkers() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}